#!/bin/sh
#
#

. scripts/functions

TestName="action-on-purge-test"
JobName=FIFOTest

# Directory to backup.
# This directory will be created by setup_data().
# If set, $BackupDirectory will be used
# as base directory by check_restore_diff().
# Don't use a "/" at the end of the directory path,
# as it might cause problems
# (filenames given as parameter to restore must match 100%. "//" are not identical with "/").
BackupDirectory="${tmp}/data"


scripts/cleanup
scripts/copy-test-confs

sed 's/Pool Type = Backup/Pool Type = Backup; ActionOnPurge = Truncate/' $conf/susan-dir.conf > $tmp/1
cp $tmp/1 $conf/susan-dir.conf

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data".
setup_data data/small.tgz


start_test

cat >tmp/bconcmds <<END_OF_DATA
@$out /dev/null
messages
@$out $tmp/log1.out
label storage=File volume=TestVolume001
@########################################################
@# Run a first job on TestVolume001
@########################################################
run level=full job=$JobName yes
wait
messages
@#@$out $tmp/log2.out
@#restore where=${cwd}/tmp/susan-restores select all storage=File done
@#yes
@#wait
@#messages
@#########################################################
@# Run a second job on TestVolume002
@#########################################################
@$out $tmp/log3.out
update volume=TestVolume001 volstatus=Used
label storage=File volume=TestVolume002
messages
show pool
@$out $tmp/log1.out
run level=full job=$JobName yes
wait
messages
@#########################################################
@# Run a 3th job on TestVolume003
@#########################################################
@$out $tmp/log3.out
update volume=TestVolume002 volstatus=Used
label storage=File volume=TestVolume003
messages
show pool
@$out $tmp/log1.out
run level=full job=$JobName yes
wait
messages
@#########################################################
@# Run a 4th job
@#########################################################
@$out $tmp/log5.out
update volume=TestVolume003 volstatus=Used
label storage=File volume=TestVolume004
messages
@$out $tmp/log1.out
run level=full job=$JobName yes
wait
messages
@#########################################################
@# Run a 5th job
@#########################################################
@$out $tmp/log1.out
update volume=TestVolume004 volstatus=Used
label storage=File volume=TestVolume005
run level=full job=$JobName yes
wait
messages
list volume
END_OF_DATA

run_susan
check_for_zombie_jobs storage=File

cat >tmp/bconcmds <<END_OF_DATA
@$out $tmp/log6.out
@#########################################################
@# purge media 1-4, without action.
@#########################################################
update volume=TestVolume002 actiononpurge=none
purge volume=TestVolume001
purge volume=TestVolume002
purge volume=TestVolume003
purge volume=TestVolume004
list volume
sql
select VolumeName, VolStatus, ActionOnPurge FROM Media;

setdebug level=100 director
quit
END_OF_DATA
run_bconsole

print_debug "Check if all media contain data"
perl -Mscripts::functions -e 'foreach $i (1..5){check_min_volume_size(4096,"TestVolume00$i")}'

if [ $? != 0 ]; then
    print_debug "`ls -l $tmp`"
    set_error "not all volumes contain data"
fi

cat >tmp/bconcmds <<END_OF_DATA
@#########################################################
@# purge volume 1 and perform action.
@#########################################################
purge volume=TestVolume001 action storage=File
END_OF_DATA

run_bconsole

perl -Mscripts::functions -e '
check_min_volume_size(4096, "TestVolume002","TestVolume003","TestVolume004","TestVolume005");
check_max_volume_size(4096, "TestVolume001")'

if [ $? != 0 ]; then
    print_debug "`ls -l $tmp/TestVolume*`"
    set_error "purging volume 1 did fail"
fi

# V1: already truncated
# V2: action=None, no action should be performed
# V3: truncate
# V4: truncate
# V5: not purged. Purge is only performed if a specific volume is given.
#     However, truncate is performed non the less (on a purged volume).
cat >tmp/bconcmds <<END_OF_DATA
@#########################################################
@# purge all volumes (1-5) and perform action.
@# This should affect volume 3 and 4.
@#########################################################
purge volume action storage=File
END_OF_DATA
run_bconsole

sleep 1;

perl -Mscripts::functions -e '
check_min_volume_size(4096, "TestVolume002", "TestVolume005");
check_max_volume_size(4096, "TestVolume001", "TestVolume003", "TestVolume004")'

if [ $? != 0 ]; then
    print_debug "`ls -l $tmp/TestVolume*`"
    set_error "purging all volumes did fail."
fi


cat <<END_OF_DATA > $tmp/bconcmds
@#########################################################
@# Display catalog settings for Pool and Media
@#########################################################
@$out $tmp/log4.out
setdebug level=0 director
sql
select VolumeName, ActionOnPurge FROM Media;
select Name, ActionOnPurge FROM Pool;

wait
quit
END_OF_DATA

run_bconsole

stop_susan

touch $tmp/log2.out
check_two_logs

print_debug "Test if Pool record is ok"
r=`awk '/Default/ { print $4 }' $tmp/log4.out`
if [ "$r" != 1 ]; then
    print_debug "ActionOnPurge on Pool record should be 1 ($r)"
    estat=2
fi

print_debug "Test TestVolume001 if Media record is ok"
r=`awk '/TestVolume001/ { print $4 }' $tmp/log4.out`
if [ "$r" != 1 ]; then
    print_debug "ActionOnPurge on Media record should be 1"
    estat=2
fi

print_debug "Test TestVolume002 if Media record is ok"
r=`awk '/TestVolume002/ { print $4 }' $tmp/log4.out`
if [ "$r" != 0 ]; then
    print_debug "ActionOnPurge on Media record should be 0"
    estat=2
fi

end_test
